<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Law;
use App\Model\LawCategory;
use App\Model\LawDetail;
use App\Model\Checkcategory;
use Auth;

class LawController extends Controller
{


    protected $nine_categories = array();
    public function __construct()
    {
        $this->nine_categories = array(array('id'=>0, 'name'=>'通用'));
    }
    
    /*
    *法律法规分类页面
    */
    public function category(Request $request)
    {
        $type = $request->input('type');
        /*获取分类*/
        $categories = LawCategory::get();
        $categories = json_decode(json_encode($categories), true);
        $categories = array_column($categories,NULL,'flag');

        return view('law/category', [
            'categories' => $categories,
            'type' => $type
        ]);
    }
    /*
    *法律法规九小场所选择
    */
    public function nine(Request $request)
    {
        $categories = Checkcategory::orderBy('sort', 'ASC')->get();
        $categories = json_decode(json_encode($categories), true);
        $categories = array_merge($this->nine_categories, $categories);
        return view('law/nine', [
            'categories' => $categories
        ]);
    }
    /*
    *法律法规列表
    *param type 分类id 
    */
    public function listt(Request $request)
    {
        $checkCategoryId = $request->input('type', 0);
        $categoryId = $request->input('category');
        $title = $request->input('title');

        $where = array();
        $where[] = array('categoryId', $categoryId);
        $where[] = array('checkCategoryId', $checkCategoryId); 
        if(!empty($title)) {
            $where[] = array('title', 'like', '%'.$title.'%');
        }
        $list = Law::where($where)->orderBy('updateTime', 'DESC')->get();
        if(!empty($title)) {
            foreach($list as $k=>$v) {
                $list[$k]['title'] = preg_replace("/($title)/i", 
                                "<font color=red><b>\\1</b></font>", 
                                $v['title']); 
            }
        }

        /*根据用户判断是否有下载权限*/
        $user = Auth::user();
        $download_show = 1;
        if($user['type']!==1) {
            $download_show = 0;
        }

        return view('law/list', [
            'list' => $list,
            'download_show' => $download_show,
            'type' => $checkCategoryId,
            'category' => $categoryId,
            'title' => $title
        ]);
    }

    /*
    *法律法规详情
    *param id 法律法规id
    */
    public function detail(Request $request)
    {
        $id = $request->input('id');
        
        $law = Law::where('id', $id)->first();

        $law_detail = LawDetail::where('id', $id)->first();
        $law_detail['content'] = html_entity_decode($law_detail['content']);

        /*浏览量+1*/
        Law::where('id', $id)->update(array('hits'=>$law['hits']+1));

        return view('law/detail', [
            'law' => $law,
            'law_detail' => $law_detail
        ]);
    }

    /*
    *法律法规下载
    *param id 法律法规id
    */
    public function downLoad(Request $request)
    {
        $id = $request->input('id');
        /*验证是否有权限下载*/
        $user = Auth::user();
        if($user['type']==1) {
            $law = Law::where('id', $id)->first();
            $file = '.'.$law['downUrl'];
            if(file_exists($file)){
                header("Content-Type: application/force-download;"); //告诉浏览器强制下载
                header("Content-type:application/octet-stream");
                $filename = $law['fileName'];
                header("Content-Disposition:attachment;filename = ".$filename);
                header("Accept-ranges:bytes");
                header("Accept-length:".filesize($file));
                readfile($file);
            }else{
                \Session::flash('error', '文件不存在!');
                return back();
            } 
        } else {
            \Session::flash('error', '您没有权限');
            return back();
        }
    }
}
